package com.xingongchang.baolebei.model;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.SharedPreferences;


import com.external.androidquery.callback.AjaxStatus;
import com.insthub.BeeFramework.model.BaseModel;
import com.insthub.BeeFramework.model.BeeCallback;

import com.xingongchang.baolebei.fragment.ProfileFragment;
import com.xingongchang.baolebei.protocol.STATUS;
import com.xingongchang.baolebei.protocol.USER;


public class LogoutModel extends BaseModel{

		private SharedPreferences shared;
		private SharedPreferences.Editor editor;
		
		public LogoutModel(Context context) {
			super(context);
			shared = context.getSharedPreferences("userInfo", 0); 
			editor = shared.edit();
		}

		public STATUS responseStatus;
		public void logout(String uid, String sid) {
			String url = ProtocolConst.CANAEL;
			final String  uuid = uid;
			BeeCallback<JSONObject> cb = new BeeCallback<JSONObject>() {

				@Override
				public void callback(String url, JSONObject jo, AjaxStatus status) {

					LogoutModel.this.callback(url, jo, status);
					try {
						
						responseStatus = STATUS.fromJson(jo.optJSONObject("status"));
						
						USER user = ProfileFragment.userInfo(uuid);
						user.delete();
							editor.putString("uid", "");
		                    editor.putString("sid", "");
		                    editor.commit();

						LogoutModel.this.OnMessageResponse(url, jo, status);
						
					} catch (JSONException e) {
						 
						e.printStackTrace();
					}
				}

			};

			Map<String, String> params = new HashMap<String, String>();
			JSONObject json = new JSONObject();
			
			try {
				json.put("sid", sid);
				json.put("uid", uid);
				JSONObject jso = new JSONObject();
				jso.put("session", json);
				params.put("json", jso.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

			cb.url(url).type(JSONObject.class).params(params);
			aq.ajax(cb);

		}
	}


